const express = require('express');
const cart = express.Router();
const {Cart,Food} = require('../db/mongoose');

// 获取当前用户的购物车
cart.post('/getUserCart',(req,res)=>{
    let user_id = req.body.user_id;
    Cart.find({
        user_id:user_id
    },(err,data)=>{
        if(err) return res.status(200).json({
            code:'6001',
            message:'系统错误'
        })
        if(data.length == 0) return res.status(200).json({
            code:'6101',
            message:'获取当前用户的购物车失败'
        })
        return res.status(200).json({
            code:'6100',
            message:'获取当前用户的购物车成功',
            data
        })
    }).populate('food_id').populate('shop_id');
})

// 获取当前用户的指定店铺的购物车
cart.post('/getUserShopCart',(req,res)=>{
    let shop_id = req.body.shop_id;
    let user_id = req.body.user_id;
    console.log('getUserShopCart',req.body);
    Cart.find({
        shop_id:shop_id,
        user_id:user_id
    },(err,data)=>{
        if(err) return res.status(200).json({
            code:'6001',
            message:'系统错误'
        })
        if(data.length == 0) return res.status(200).json({
            code:'6201',
            message:'获取当前用户的指定店铺的购物车失败'
        })
        // console.log('getUserShopCart',data);
        console.log('getUserShopCart',data);
        return res.status(200).json({
            code:'6200',
            message:'获取当前用户的指定店铺的购物车成功',
            data
        })
    }).populate('food_id').populate('shop_id');
})

// 添加购物车
cart.post('/addCart',(req,res)=>{
    let cart = req.body;
    if(cart.foodSpec == undefined) cart.foodSpec = [];
    console.log('addCart',cart);

    let newCart = new Cart(cart);
    newCart.save(err=>{
        if(err) return res.status(200).json({
            code:'6001',
            message:'系统错误'
        })
        return res.status(200).json({
            code:'6300',
            message:'添加购物车成功'
        })
    })
})

// 修改购物车信息
cart.post('/modifyCart',(req,res)=>{
    let cart = req.body;
    if(cart.foodSpec == undefined) cart.foodSpec = [];
    console.log('modifyCart',cart);
    Cart.updateOne({
        shop_id:cart.shop_id,
        user_id:cart.user_id,
        food_id:cart.food_id,
    },{
        foodSpec:cart.foodSpec,
        foodNum:parseInt(cart.foodNum)
    },(err,result)=>{
        if(err) return res.status(200).json({
            code:'6001',
            message:'系统错误'
        })
        console.log(result)
        if(result.modifiedCount == 0) return res.status(200).json({
            code:'6401',
            message:'未找到匹配数据'
        })
        return res.status(200).json({
            code:'6400',
            message:'修改购物车数据成功'
        })
    })
})

// 修改购物车 商品数量
cart.post('/modifyCartNum',(req,res)=>{
    let cart = req.body;
    if(cart.foodSpec == undefined) cart.foodSpec = [];
    console.log('modifyCart',cart);
    Cart.updateOne({
        _id:cart.id,
    },{
        foodNum:cart.num
    },(err,result)=>{
        if(err) return res.status(200).json({
            code:'6001',
            message:'系统错误'
        })
        console.log(result)
        if(result.modifiedCount == 0) return res.status(200).json({
            code:'6401',
            message:'未找到匹配数据'
        })
        return res.status(200).json({
            code:'6400',
            message:'修改购物车中 指定的一条商品成功'
        })
    })
})

// 删除购物车中 指定的一条商品
cart.post('/deleteFoodCart',(req,res)=>{
    let cart = req.body;
    console.log('deleteFoodCart',cart);
    Cart.deleteOne({
        _id:cart.id
    },(err,result)=>{
        if(err) return res.status(200).json({
            code:'6001',
            message:'系统错误'
        })
        console.log(result)
        if(result.deletedCount == 0) return res.status(200).json({
            code:'6501',
            message:'未找到匹配数据'
        })
        return res.status(200).json({
            code:'6500',
            message:'删除购物车中 指定的一条商品成功'
        })
    })
})

// 删除指定店铺内所有商品
cart.post('/deleteShopCart',(req,res)=>{
    let user_id = req.body.user_id;
    let shop_id = req.body.shop_id;
    console.log('deleteShopCart',user_id,shop_id);
    Cart.deleteMany({
        user_id:user_id,
        shop_id:shop_id
    },(err,result)=>{
        if(err) return res.status(200).json({
            code:'6001',
            message:'系统错误'
        })
        console.log(result)
        if(result.deletedCount == 0) return res.status(200).json({
            code:'6601',
            message:'未找到匹配数据'
        })
        return res.status(200).json({
            code:'6600',
            message:'删除指定店铺内所有商品成功'
        })
    })
})

module.exports = cart;